﻿using Digitalmes.Model;
using Digitalmes.Model.FactoryManagement;
using Digitalmes.Model.ProcessManagement;
using Digitalmes.Model.ProductManagement;
using Digitalmes.Model.WmsManagement;
using Digitalmes.ViewModel.ProductManagement.ProductVMs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc;

namespace Digitalmes.ProductManagement.Controllers
{
    [AuthorizeJwtWithCookie]
    [ActionDescription("_Model.Product")]
    [ApiController]
    [Route("/api/ProductManagement/Product")]
    public partial class ProductController : BaseApiController
    {
        [ActionDescription("Sys.Get")]
        [HttpGet("{id}")]
        public IActionResult Get(string id)
        {
            var vm = Wtm.CreateVM<ProductVM>(id);
            return Ok(vm);
        }

        [ActionDescription("Sys.Create")]
        [HttpPost("[action]")]
        public async Task<IActionResult> Create(ProductVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoAddAsync();
   
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }

        }

        [ActionDescription("Sys.Edit")]
        [HttpPut("[action]")]
        public async Task<IActionResult> Edit(ProductVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoEditAsync(false);
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }
                

        [HttpPost("BatchEdit")]
        [ActionDescription("Sys.BatchEdit")]
        public ActionResult BatchEdit(ProductBatchVM vm)
        {
            if (!ModelState.IsValid || !vm.DoBatchEdit())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(vm.Ids.Count());
            }
        }

		[HttpPost("BatchDelete")]
        [ActionDescription("Sys.Delete")]
        public IActionResult BatchDelete([FromBody] string[] ids)
        {
            var vm = Wtm.CreateVM<ProductBatchVM>();
            if (ids != null && ids.Count() > 0)
            {
                vm.Ids = ids.Select(x=>x.ToString()).ToArray();
            }
            else
            {
                return Ok();
            }
            if (!ModelState.IsValid || !vm.DoBatchDelete())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(ids.Count());
            }
        }

        [ActionDescription("Sys.DownloadTemplate")]
        [HttpGet("GetExcelTemplate")]
        public IActionResult GetExcelTemplate()
        {
            var vm = Wtm.CreateVM<ProductImportVM>();
            var qs = new Dictionary<string, string>();
            foreach (var item in Request.Query.Keys)
            {
                qs.Add(item, Request.Query[item]);
            }
            vm.SetParms(qs);
            var data = vm.GenerateTemplate(out string fileName);
            return File(data, "application/vnd.ms-excel", fileName);
        }

        [ActionDescription("Sys.Import")]
        [HttpPost("Import")]
        public ActionResult Import(ProductImportVM vm)
        {

            if (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData())
            {
                return BadRequest(vm.GetErrorJson());
            }
            else
            {
                return Ok(vm.EntityList.Count);
            }
        }


        [Public]
        [HttpGet("GetProductProcessItems")]
        public ActionResult GetProductProcessItems(long id)
        {
            var lst = new List<ComboSelectListItem>();

            var router = DC.Set<ProcessRoute>().Where(x => x.ProductId == id).FirstOrDefault();
            if (router != null)
            {
                return Ok(DC.Set<ProcessRouteItem>().CheckID(router.ID,x=>x.RoutieId).GetSelectListItems(Wtm, x => x.OpCode));
            }

            return Ok(lst);
        }



        [Public]
        [HttpGet("GetMaterialDocs")]
        public ActionResult GetMaterialDocs()
        {
            var lst= DC.Set<MaterialDoc>().GetSelectListItems(Wtm, x => ( x.Name  + x.Code));
            return Ok(lst);
        }
        [Public]
        [HttpPost("[action]")]
        public ActionResult Select_GetMaterialDocByMaterialDocId(List<string> id)
        {
            var rv = DC.Set<MaterialDoc>().CheckIDs(id).GetSelectListItems(Wtm, x => x.Name);
            return Ok(rv);
        }
        [Public]
        [HttpPost("[action]")]
        public ActionResult Select_GetProductByMaterialDoc(List<string> id)
        {
            var rv = DC.Set<Product>().CheckIDs(id, x => x.MaterialId).GetSelectListItems(Wtm,x=>x.Name);
            return Ok(rv);
        }

        [Public]
        [HttpPost("[action]")]
        public ActionResult Select_GetProductionLineByProduct(List<string> id)
        {
            var tempid = DC.Set<ProductProductionLine>().CheckIDs(id, x => x.ProductId).Select(x => x.ProductionLineId.ToString()).ToList();
            var rv = DC.Set<ProductionLine>().CheckIDs(tempid).GetSelectListItems(Wtm, x => x.Name);
            return Ok(rv);
        }

    }
}